package book.action;

import static common.utils.Constants.LIMIT;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import member.service.impl.MemberServiceImpl;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import common.action.BaseAction;
import common.pojo.Member;
import common.utils.Constants;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import common.utils.SessionUtils;

@Scope("prototype")
@Controller(value = "member")
public class MemberAction extends BaseAction {

	
	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getMemberListByPage() throws Exception {
		String currentTmp = request.getParameter("current");//获取页码参数
		Member m = new Member();
		m.setDeleted(Constants.NOT_DELETED);
		int total= mService.queryByCount(m);//查询符合条件的类型数量
		int current = PageUtil.getCurrent(currentTmp);//判断当前页码数
		Page page = PageUtil.createPage(LIMIT, total, current);//生成页码实体参数
		memberlist = mService.find(page.getBeginIndex(), LIMIT);//根据页码参数以及每页显示数量获取列表
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getMemberListByPage.action","");//页码显示的静态代码，字符串，很长
		return "memberList";
	}


	/**
	 * 删除类型
	 */
	public String deleteById() {
		String ids = request.getParameter("ids");
		mService.delete(ids,SessionUtils.getAdminName(request));
		return SUCCESS;
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String add() {
		mService.add(member);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return SUCCESS;
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update() {

		member.setUpdateName(SessionUtils.getAdminName(request));
		member.setUpdateTime(new Date());
		mService.update(member);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return SUCCESS;
	}
	
	/**
	 * 按id查询
	 * @return
	 */
	public String findInfoById() {
		String id = request.getParameter("id");
		member=mService.findInfoById(id);
		return "info";
	}
	

	
	private static final long serialVersionUID = 1L;

	//引用service层
	@Resource
	private MemberServiceImpl mService;
	private Member member;
	private List<Member> memberlist;//列表
	private String pagination;//分页
	private String dofunction;//返回处理刷新


	public Member getMember() {
		return member;
	}


	public void setMember(Member member) {
		this.member = member;
	}


	public List<Member> getMemberlist() {
		return memberlist;
	}


	public void setMemberlist(List<Member> memberlist) {
		this.memberlist = memberlist;
	}


	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}



}
